package com.cmict.data.form.api.util.SubUtil;

import cn.hutool.core.util.ObjectUtil;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

/**
 * @description: el-input组件规则
 * @author: qiaoyu
 * @create: 2022-07-6 22:57
 **/
public class ElInputUtil {

	public static CommonResult rule (JSONObject personData , String value ,int number) {
		String message = null;
		Boolean redata = true;
		JSONArray fields = personData.getJSONArray("fields");
		JSONObject field = fields.getJSONObject(number);
 		JSONObject config = field.getJSONObject("__config__");
		String label = config.getString("label");
		if(ObjectUtil.isNotEmpty(personData.getString("maxlength"))){
			//最大字节
			int maxlength = Integer.parseInt(personData.getString("maxlength"));
			if(value.length() > maxlength){
				message = label+"最大长度为"+maxlength+";";
				redata=false;
				//return success( label+"最大长度为"+maxlength,false);
			}
		}
		//是否必填
		Boolean required = config.getBoolean("required");
		if(required && ObjectUtil.isEmpty(value)){
			message = label+"是必填项;";
			redata=false;
			//return success( label+"是必填项",false);
		}
		JSONObject slot = field.getJSONObject("__slot__");
		//前缀
		if(ObjectUtil.isNotEmpty(slot.getString("prepend"))){
			String prepend = slot.getString("prepend");
			if(!value.contains(prepend)){
				message = "没有包含前缀 "+prepend+";";
				redata=false;
				//return success( "没有包含前缀 "+prepend,false);
			}
		}
		//后缀
		if(ObjectUtil.isNotEmpty(slot.getString("append"))){
			String append = slot.getString("append");
			if(!value.contains(append)){
				message = "没有包含后缀 "+append+";";
				redata=false;
				//return success( "没有包含后缀 "+append,false);
			}
		}
		if(!redata){
			return success( message,false);
		}
		return success( "通过",true);
	}
}

